<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项卡</title>
    <style>
        .item {
            width: 100px;
            height: 100px;
            background-color: aqua;
            margin: 10px;
            float: left;
            display: none;
        }

        .show {
            display: block;
        }
    </style>
</head>

<body>
    <button class="btn">按钮1</button>
    <button class="btn">按钮2</button>
    <button class="btn">按钮3</button>
    <button class="btn">按钮4</button>
    <div>
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
    </div>
    <script>
        let btns = document.querySelectorAll(".btn");
        let items = document.querySelectorAll(".item");
        let nowItem = null;
        btns.forEach((btn, i) => {
            btn.onclick = () => {
                // items[i].classList.toggle("show");
                if (nowItem) {
                    nowItem.classList.remove("show");
                }
                items[i].classList.add("show");
                nowItem = items[i];
            };
        });
    </script>
</body>

</html>